/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

/**
 *
 * @author Laurent WOUTERS
 */
public class Label extends Widget {
    private Couple minSize;
    private boolean isFill;
    private java.awt.Font font;
    private java.awt.Color fontColor;
    private String text;
    
    public void setWidth(int width) { this.minSize = new Couple(width, minSize.y); }
    public void setHeight(int height) { this.minSize = new Couple(minSize.x, height); }
    public void setFontName(String name) { this.font = new java.awt.Font(name, font.getStyle(), font.getSize()); }
    public void setFontStyle(int style) { this.font = new java.awt.Font(font.getFontName(), style, font.getSize()); }
    public void setFontSize(int size) { this.font = new java.awt.Font(font.getFontName(), font.getStyle(), size); }
    public void setFontColor(java.awt.Color color) { this.fontColor = color; }
    public void setText(String text) { this.text = text; }
    
    public Label(int width, int height, boolean isFill, java.awt.Font font, java.awt.Color color, String text) {
        this.minSize = new Couple(width, height);
        this.isFill = isFill;
        this.font = font;
        this.text = text;
    }
    
    @Override public Couple getMinSize() { return minSize; }
    @Override public Couple getMaxSize() { return minSize; }
    @Override public Couple layout() {
        this.dimension = minSize;
        return minSize;
    }
    @Override public Couple layout(Couple bounds) {
        if (isFill)
            dimension = bounds;
        else
            dimension = minSize;
        return dimension;
    }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        if (font != null)
            g2d.setFont(font);
        else
            g2d.setFont(new java.awt.Font(style.fontName, style.fontStyle, style.fontSize));
        java.awt.FontMetrics fm = g2d.getFontMetrics();
        if (fontColor != null)
            g2d.setColor(getRealColor(fontColor, alpha));
        else
            g2d.setColor(getRealColor(java.awt.Color.BLACK, alpha));
        Couple r = position.plus(offset);
        g2d.setStroke(new java.awt.BasicStroke(1));
        String txt = (text != null)?text:"";
        g2d.drawString(txt, (int)(r.x + (dimension.x - fm.stringWidth(txt)) / 2), (int)(r.y + (dimension.y - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent()));
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        java.awt.FontMetrics fm = null;
        if (font != null) fm = getFontMetrics(font);
        else fm = getFontMetrics(new java.awt.Font(style.fontName, style.fontStyle, style.fontSize));
        java.awt.Color color = getRealColor(fontColor!=null?fontColor:java.awt.Color.BLACK, alpha);
        String txt = (text != null)?text:"";
        double x = r.x + (dimension.x - fm.stringWidth(txt)) / 2;
        double y = r.y + (dimension.y - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent();
        org.w3c.dom.Element elem = doc.createElement("text");
        elem.setAttribute("x", Double.toString(x));
        elem.setAttribute("y", Double.toString(y));
        elem.setAttribute("textLength", Double.toString(fm.stringWidth(txt)));
        elem.setAttribute("font-family", fm.getFont().getFamily());
        elem.setAttribute("font-size", Float.toString(fm.getFont().getSize2D()));
        if ((fm.getFont().getStyle() & java.awt.Font.BOLD) == java.awt.Font.BOLD)
            elem.setAttribute("font-weight", "bold");
        if ((fm.getFont().getStyle() & java.awt.Font.ITALIC) == java.awt.Font.ITALIC)
            elem.setAttribute("font-style", "italic");
        elem.setAttribute("fill", "#" + getColorString(color));
        if (color.getAlpha() != 255) {
            float value = ((float)color.getAlpha()) / 255;
            elem.setAttribute("fill-opacity", Float.toString(value));
        }
        elem.appendChild(doc.createTextNode(txt));
        parent.appendChild(elem);
    }
}
